* Coal production regressions for "Green Expectations: Current Effects of Anticipated Carbon Pricing".

clear
capture log close
estimates clear

local dir="C:/yourdirectory"  /* main directory */
local dir_input = "`dir'"  /* directory with input data file */

local dir_output="`dir'/output/coal_prod"  /* main directory */

log using "`dir'/logs/coalprod.log", replace

set more off
set matsize 1000


*_______________________________________________________________________________


/* Get monthly production and consumption data (thousand short tons) */
   
use "`dir_input'/eia_coalprodcons.dta", clear

tsset Counter

gen delta_prod = Production-L.Production
gen pct_prod = ln(Production/L.Production)

gen eventmonth = 0
replace eventmonth = 1 if (Year==2010 & Month==4)

* Production regressions
xi: ivreg2 pct_prod eventmonth, kernel(bartlett) bw(auto) small	
estimates store nofe
xi: ivreg2 pct_prod eventmonth i.Month, kernel(bartlett) bw(auto) small	
estimates store monthfe
xi: ivreg2 pct_prod eventmonth i.Month i.Year, kernel(bartlett) bw(auto) small	
estimates store yearfe


local specif_list "nofe monthfe yearfe"

foreach specif of local specif_list{
	/* obtain residuals so can do SQ test and normality tests */
	estimates restore `specif'
	predict resid_`specif', residual
	*egen std_resid_`specif'  = std(resid_`specif')

	* save residuals for taking into Excel
	save "`dir_output'/temp_results.dta", replace
	drop if eventmonth==1
	keep resid_*
	*xpose, clear varname
	save "`dir_output'/resids.dta", replace
	use "`dir_output'/temp_results.dta"  /* get all variables back */	
	
	drop if eventmonth==1
	* Shapiro-Wilk test for normality
	swilk resid_`specif'
	* Jarque-Bera test for normality
	jb6 resid_`specif'
	use "`dir_output'/temp_results.dta", clear  /* get all variables back */	
}

drop if eventmonth==1


/* Plot density of residuals and original variable*/
kdensity resid_yearfe, generate(x_resid_base y_resid_base) nograph			
keep x_* y_*
save "`dir_output'/kdensity_base.dta", replace
use "`dir_output'/temp_results.dta"  /* get all variables back */	
drop if eventmonth==1
kdensity pct_prod, generate(x_var y_var) nograph			
keep x_* y_*
save "`dir_output'/kdensity_originalvar.dta", replace
use "`dir_output'/temp_results.dta"  /* get all variables back */	

log close
